package com.ld.explanation;

import com.intellij.openapi.editor.Editor;

public class ZHExplanation extends TextExplanation{

    public ZHExplanation(Editor editor) {
        this.editor = editor;
    }

    @Override
    public String translation() {
        String selectedText = getSelectedText();
        String cache = translationCache.get(selectedText);
        if (cache != null){
            return cache;
        }
        String[] strings = selectedText.split("[a-zA-Z]");
        for (String str:strings){
            int length = str.trim().length();
            if (length == 0){
                continue;
            }
            try {
                String zhToEn = zhToEn(str);
                selectedText = selectedText.replace(str," "+zhToEn+" ");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!isAComment()){
            switch (splicing){
                case "tf":
                    selectedText = hump(selectedText);
                    break;
                case "_":
                    selectedText = underline(selectedText);
                    break;
                default:
                    selectedText = hump(selectedText);
                    break;
            }
        }
        selectedText = selectedText.trim();
        translationCache.put(editor.getSelectionModel().getSelectedText(),selectedText);
        translationCache.put(selectedText,editor.getSelectionModel().getSelectedText());
        return selectedText;
    }

    private boolean isAComment(){
        String text = editor.getDocument().getText();
        text = text.substring(0,editor.getCaretModel().getOffset());
        String[] split = text.split("\n");
        String s1 = split[split.length - 1];
        String trim = s1.trim();
        boolean b = trim.startsWith("//");
        if (!b){
            b = trim.startsWith("*");
            if (!b){
                b = trim.startsWith("/*");
            }
        }
        return b;
    }

    private String hump(String v){
        StringBuilder sb = new StringBuilder();
        for (String s : v.split(" ")) {
            String trim = s.trim();
                trim = trim.toLowerCase();
            if (trim.length() == 0){
                continue;
            }
            if (sb.length() > 0){
                trim = upperCase(trim);
            }
            sb.append(trim);
        }
        return sb.toString();
    }

    private String underline(String v){
        StringBuilder sb = new StringBuilder();
        for (String s : v.split(" ")) {
            String trim = s.trim();
            trim = trim.toLowerCase();
            if (trim.length() == 0){
                continue;
            }
            sb.append(trim).append("_");
        }
        String s = sb.toString();
        return s.substring(0,s.length()-1);
    }

    private String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }
}
